package com.zjsd.sqeybusiness.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.zjsd.sqeybusiness.domain.Dlaccount;
import com.zjsd.sqeybusiness.domain.Dlpayment;
import com.zjsd.sqeybusiness.dto.AccountDTO;
import com.zjsd.sqeybusiness.dto.AchDetailDTO;
import com.zjsd.sqeybusiness.mapper.DlaccountMapper;
import com.zjsd.sqeybusiness.mapper.DlpaymentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.mapper.DkktapplyMapper;
import com.zjsd.sqeybusiness.domain.Dkktapply;
import com.zjsd.sqeybusiness.service.IDkktapplyService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Zheng
 * @date 2022-05-12
 */
@Service
public class DkktapplyServiceImpl implements IDkktapplyService
{
    @Autowired
    private DkktapplyMapper dkktapplyMapper;

    @Autowired
    private DlaccountMapper dlaccountMapper;

    @Autowired
    private DlpaymentMapper dlpaymentMapper ;
    /**
     * 查询【请填写功能名称】
     *
     * @param applyid 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public Dkktapply selectDkktapplyById(Long applyid)
    {
        return dkktapplyMapper.selectDkktapplyById(applyid);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param dkktapply 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Dkktapply> selectDkktapplyList(Dkktapply dkktapply)
    {
        return dkktapplyMapper.selectDkktapplyList(dkktapply);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param dkktapply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDkktapply(Dkktapply dkktapply)
    {
        dkktapply.setShbz("0");
        dkktapply.setSqtime(new Date());
        return dkktapplyMapper.insertDkktapply(dkktapply);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param dkktapply 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDkktapply(Dkktapply dkktapply)
    {
        return dkktapplyMapper.updateDkktapply(dkktapply);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param applyids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDkktapplyByIds(Long[] applyids)
    {
        return dkktapplyMapper.deleteDkktapplyByIds(applyids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param applyid 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDkktapplyById(Long applyid)
    {
        return dkktapplyMapper.deleteDkktapplyById(applyid);
    }

    @Override
    public int examineDkktapply(Dkktapply dkktapply) {

        dkktapply.setShtime(new Date());

        List<AccountDTO> list = dkktapply.getAccountList() ;
        if(list != null){
            if(list.size() > 0 ){
                for(AccountDTO temp : list){
                    if(temp.getKktotal().compareTo(BigDecimal.ZERO) > 0 ){
                        Dlpayment dlpayment  = new Dlpayment() ;
                        dlpayment.setBdlx("2");
                        dlpayment.setCzje(temp.getKktotal());
                        dlpayment.setDeptid(dkktapply.getDeptid());
                        dlpayment.setDeptname(dkktapply.getDeptname());
                        dlpayment.setFssj(new Date());
                        dlpayment.setBz("客户" + dkktapply.getKhname() + "开通端口，" + temp.getTcname() + "扣款" + temp.getKktotal() + "元");
                        dlpayment.setTcid(temp.getTcid());
                        Dlaccount dlaccount = new Dlaccount();
                        dlaccount.setDeptid(dkktapply.getDeptid());
                        dlaccount.setTcid(temp.getTcid());
                        List<Dlaccount> dlaccountList = dlaccountMapper.selectDlaccountList(dlaccount);

                        BigDecimal bdqje = BigDecimal.ZERO ;
                        BigDecimal bdhje = BigDecimal.ZERO ;

                        if(dlaccountList.size() > 0 ){
                            BigDecimal zhye = dlaccountList.get(0).getZhye() ;
                            bdqje = zhye ;

                            if("2".equals(dlpayment.getBdlx())){
                                bdhje = bdqje.subtract(dlpayment.getCzje());
                            }
                            dlaccountList.get(0).setZhye(bdhje);
                            dlaccountMapper.updateDlaccount(dlaccountList.get(0));
                        }
                        dlpayment.setBdhje(bdhje);
                        dlpayment.setBdqje(bdqje);
                        dlpayment.setBdqkhsjje(BigDecimal.ZERO);
                        dlpayment.setBdhkhsjje(BigDecimal.ZERO);
                        dlpaymentMapper.insertDlpayment(dlpayment );
                    }
                }
            }
        }
        int i = dkktapplyMapper.examineDkktapply(dkktapply);
        return i;
    }
}
